package com.shop.shopserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shop.shopcommon.context.BaseContext;
import com.shop.shopcommon.context.SecurityContext;
import com.shop.shoppojo.dto.AddressDTO;
import com.shop.shoppojo.entity.Address;
import com.shop.shoppojo.entity.User;
import com.shop.shoppojo.vo.AddressVO;
import com.shop.shopserver.mapper.AddressMapper;
import com.shop.shopserver.mapper.UserMapper;
import com.shop.shopserver.service.AddressService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * 新增地址
     * @param addressDTO
     */

    public void save(AddressDTO addressDTO) {
        User user = getUserIdByUsername();
        if (user != null) {
            addressDTO.setUserId(user.getId());
            if (addressDTO.getIsDefault()) {
                setDefault(addressDTO);
            }
            addressMapper.insert(addressDTO) ;
        }
    }

    /**
     * 根据id更新地址
     * @param addressDTO
     */
    public void update(AddressDTO addressDTO) {
        if (addressDTO.getIsDefault()) {
            setDefault(addressDTO);
        }
        addressMapper.update(addressDTO);
    }

    /**
     * 获取地址
     * @return
     */

    public List<AddressVO> list() {
        Address address = new Address();
        User user = getUserIdByUsername();
        //获得了用户id然后插入address
        address.setUserId(user.getId());
        //调用mapper层根据userId查询相关地址
        List<Address> addressList = addressMapper.list(address);
        //使用vo类型来加入全称地址，在vo类型里面设置address字段，将address字段值赋值给addressVOList
        List<AddressVO> addressVOList = new ArrayList<>();
        for (Address add : addressList) {
            AddressVO addressVO = new AddressVO();
            //拷贝其他字段值
            BeanUtils.copyProperties(add, addressVO);
            //拼接生成address字段值
            addressVO.setAddress(add.getProvince() + add.getCity() + add.getCounty() + add.getAddressDetail());
            addressVOList.add(addressVO);
        }
        return addressVOList;
    }

    // 获取当前登录用户id私有方法
    private User getUserIdByUsername() {
        String username = SecurityContext.getUsername();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
                .select(User::getId)
                .eq(User::getUsername, username);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    /**
     * 删除地址
     * @param id
     */
    public void delete(Long id) {
        addressMapper.deleteById(id);
    }


    /**
     * 设置默认地址
     */
    public void setDefault(AddressDTO addressDTO) {
        //1、将当前用户的所有地址修改为非默认地址
        addressDTO.setIsDefault(false);
        User user = getUserIdByUsername();
        addressDTO.setUserId(user.getId());
        addressMapper.updateIsDefaultByUserId(addressDTO);
        //2、将当前地址改为默认地址
        addressDTO.setIsDefault(true);
        addressMapper.update(addressDTO);
    }



}
